﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class SearchResults : System.Web.UI.Page
    {
        private List<Applicant> _allApplicantsFromSearch;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["SelectedCv"] = new List<int>();
                if (Session["SearchString"] != null)
                {
                    var searchStringList = Session["SearchString"] as List<object>;

                    string searchString = searchStringList[0].ToString();
                    int selection = int.Parse(searchStringList[1].ToString());
                    switch (selection)
                    {
                        case 0:
                            {
                                _allApplicantsFromSearch = SearchDB.SearchForEverything(searchString);
                                break;
                            }
                        case 1:
                            {
                                _allApplicantsFromSearch = SearchDB.SearchAllMeritsRanked(searchString);
                                break;
                            }
                        case 2:
                            {
                                _allApplicantsFromSearch = SearchDB.SearchAllEducations(searchString);
                                break;
                            }
                        case 3:
                            {
                                _allApplicantsFromSearch = SearchDB.SearchAllExperiencesRanked(searchString);
                                break;
                            }
                    }

                    ListViewCv.DataSource = _allApplicantsFromSearch;
                    ListViewCv.DataBind();
                }
            }


        }

        protected void ButtonCompareCv_Click(object sender, EventArgs e)
        {
            if (Session["SelectedCv"] != null)
            {
Response.Redirect("~/CompareCv.aspx");
            }
        }

        protected void ListViewCv_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                if (Session["SelectedCv"] != null)
                {
                    var selectedCv = Session["SelectedCv"] as List<int>;
                    var listViewItem = e.Item as ListViewItem;

                    var applicantId = int.Parse(ListViewCv.DataKeys[listViewItem.DataItemIndex].Value.ToString());
                    if (selectedCv.Count < 3)
                    {
                        var button = e.CommandSource as Button;
                        button.Enabled = false;
                        selectedCv.Add(applicantId);
                        Session["SelectedCv"] = selectedCv;
                    }
                    else
                    {
                        LabelToManySelected.Visible = true;
                    }
                }

            }
            catch (Exception ex)
            {
                //LabelException.Text = ex.Message;
            }
        }
    }
}